const {Op} = require('sequelize')
const typeModel = require('../models/typeModel')

module.exports = {
    getAllTypes: async() => {
        let whereObj = {}
        try{
            let types = await typeModel.findAll({
                where:whereObj,
                order: [['id','ASC']]// 升序排序
            })
            return types
        } catch(error) {
            console.log("find all type error:",error)
            return false
        }
    },
    createType: async(typeName) => {
        try {
            let result = await typeModel.create({
                typeName
            })
            return result
        } catch(error) {
            console.log("create type error:",error)
            return null
        }
    },
    deleteTypeById: async(id) => {
        try {
            let result = await typeModel.destroy({
                where: {
                    id
                }
            })
            return result
        } catch(error) {
            console.log("delete type by id error:",error)
            return false
        }
    }, 
    updateType: async(id,typeName) => {
        try {
            let result = await typeModel.update({
                typeName
            },{
                where: {
                    id
                }
            })
            return result
        } catch(error) {
            console.log("update type error:",error)
            return null
        }
    }
}